<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="考试ID" prop="examId">
        <el-input v-model="formData.examId" placeholder="请输入考试ID" />
      </el-form-item>
      <el-form-item label="学生ID" prop="studentId">
        <el-input v-model="formData.studentId" placeholder="请输入学生ID" />
      </el-form-item>
      <el-form-item label="学号" prop="studentNo">
        <el-input v-model="formData.studentNo" placeholder="请输入学号" />
      </el-form-item>
      <el-form-item label="学生姓名" prop="studentName">
        <el-input v-model="formData.studentName" placeholder="请输入学生姓名" />
      </el-form-item>
      <el-form-item label="语文成绩" prop="chineseScore">
        <el-input v-model="formData.chineseScore" placeholder="请输入语文成绩" />
      </el-form-item>
      <el-form-item label="数学成绩" prop="mathScore">
        <el-input v-model="formData.mathScore" placeholder="请输入数学成绩" />
      </el-form-item>
      <el-form-item label="英语成绩" prop="englishScore">
        <el-input v-model="formData.englishScore" placeholder="请输入英语成绩" />
      </el-form-item>
      <el-form-item label="物理成绩" prop="physicsScore">
        <el-input v-model="formData.physicsScore" placeholder="请输入物理成绩" />
      </el-form-item>
      <el-form-item label="化学成绩" prop="chemistryScore">
        <el-input v-model="formData.chemistryScore" placeholder="请输入化学成绩" />
      </el-form-item>
      <el-form-item label="生物成绩" prop="biologyScore">
        <el-input v-model="formData.biologyScore" placeholder="请输入生物成绩" />
      </el-form-item>
      <el-form-item label="政治成绩" prop="politicsScore">
        <el-input v-model="formData.politicsScore" placeholder="请输入政治成绩" />
      </el-form-item>
      <el-form-item label="历史成绩" prop="historyScore">
        <el-input v-model="formData.historyScore" placeholder="请输入历史成绩" />
      </el-form-item>
      <el-form-item label="地理成绩" prop="geographyScore">
        <el-input v-model="formData.geographyScore" placeholder="请输入地理成绩" />
      </el-form-item>
      <el-form-item label="总分" prop="totalScore">
        <el-input v-model="formData.totalScore" placeholder="请输入总分" />
      </el-form-item>
      <el-form-item label="班级排名" prop="classRank">
        <el-input v-model="formData.classRank" placeholder="请输入班级排名" />
      </el-form-item>
      <el-form-item label="年级排名" prop="gradeRank">
        <el-input v-model="formData.gradeRank" placeholder="请输入年级排名" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { ExamScoreApi, ExamScoreVO } from '@/api/system/examscore'

/** 学生成绩 表单 */
defineOptions({ name: 'ExamScoreForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  examId: undefined,
  studentId: undefined,
  studentNo: undefined,
  studentName: undefined,
  chineseScore: undefined,
  mathScore: undefined,
  englishScore: undefined,
  physicsScore: undefined,
  chemistryScore: undefined,
  biologyScore: undefined,
  politicsScore: undefined,
  historyScore: undefined,
  geographyScore: undefined,
  totalScore: undefined,
  classRank: undefined,
  gradeRank: undefined,
})
const formRules = reactive({
  examId: [{ required: true, message: '考试ID不能为空', trigger: 'blur' }],
  studentId: [{ required: true, message: '学生ID不能为空', trigger: 'blur' }],
  studentNo: [{ required: true, message: '学号不能为空', trigger: 'blur' }],
  studentName: [{ required: true, message: '学生姓名不能为空', trigger: 'blur' }],
  totalScore: [{ required: true, message: '总分不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ExamScoreApi.getExamScore(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ExamScoreVO
    if (formType.value === 'create') {
      await ExamScoreApi.createExamScore(data)
      message.success(t('common.createSuccess'))
    } else {
      await ExamScoreApi.updateExamScore(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    examId: undefined,
    studentId: undefined,
    studentNo: undefined,
    studentName: undefined,
    chineseScore: undefined,
    mathScore: undefined,
    englishScore: undefined,
    physicsScore: undefined,
    chemistryScore: undefined,
    biologyScore: undefined,
    politicsScore: undefined,
    historyScore: undefined,
    geographyScore: undefined,
    totalScore: undefined,
    classRank: undefined,
    gradeRank: undefined,
  }
  formRef.value?.resetFields()
}
</script>